package cn.tedu.ivos.vehicle.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@RestController
@RequestMapping("/v1/vehicle")
@Slf4j
@Api(tags = "车辆管理")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    @GetMapping("/select")
    @ApiOperation("查询车辆")
    public JsonResult selectVehicle(VehicleQuery vehicleQuery){
        log.debug("查询车辆参数是{}",vehicleQuery);
        List<VehicleVO> vehicleVOS = vehicleService.selectVehicle(vehicleQuery);
        return JsonResult.ok(vehicleVOS);
    }

    @PostMapping("/save")
    @ApiOperation("新增车辆")
    public JsonResult saveVehicle(VehicleSaveParam vehicleSaveParam){
        log.debug("新增车辆数据：{}",vehicleSaveParam);
        vehicleService.saveVehicle(vehicleSaveParam);
        return JsonResult.ok();
    }
    //post  http://localhost:8080/v1/vehicle/delete/{id}
    @PostMapping("/delete/{id}")
    @ApiOperation("删除车辆")
    //@PathVariable注解可以从路径中获取对应的参数值并且映射到方法列表中的参数中
    //如果方法参数列表中的参数名和路径中的参数名不一致，需要在@PathVariable注解中声明
    public JsonResult deleteVehicle(@PathVariable  Long id){
        log.debug("删除车辆的id为：{}",id);
        vehicleService.deleteVehicle(id);
        //TODO 课下练习： 12.14号自己练习用户模块的增删改查
        return JsonResult.ok();
    }
    //车辆和围栏进行解绑
    //从路径中获取值，需要一个注解：@PathVariable
    @PostMapping("/unbind/{vehicleId}")
    @ApiOperation("车辆和围栏解绑")
    public JsonResult unbindVehicle(@PathVariable Long vehicleId){
        log.debug("解绑车辆的id值为：{}",vehicleId);
        vehicleService.unBindVehicle(vehicleId);
        return JsonResult.ok();
    }
    @GetMapping("/selectByGeoId")
    @ApiOperation("根据围栏id查询对应车辆信息")
    public JsonResult selectByGeoId(Long id){
        List<VehicleVO> vehicleVOS = vehicleService.selectVehicleByGeoId(id);
        return JsonResult.ok(vehicleVOS);
    }
    //绑定车辆，路径需要携带围栏id和车辆id，post请求
    @PostMapping("/bind/{geofenceId}/{vehicleId}")
    @ApiOperation("车辆和围栏绑定")
    public JsonResult bindVehicle(@PathVariable Long geofenceId,@PathVariable Long vehicleId){
        log.debug("绑定的车辆id是{}，绑定的围栏的id是{}",vehicleId,geofenceId);
        vehicleService.bindVehicle(geofenceId,vehicleId);
        return JsonResult.ok();
    }
}
